﻿using AutoMapper;
using ManagementApi.Authorizers;
using ManagementApi.Models;
using ManagementApi.Models.System.ProjectCorpWageAuth;
using ManagementApi.Mvc;
using ManagementApi.Mvc.ApplicationContexts;
using ManagementApi.Mvc.Authorization;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Modules;
using Modules.Project;
using PmSoft.Utilities;

namespace ManagementApi.Controllers.AdminPannel
{
    /// <summary>
    /// 项目参建企业非正常工资单授权
    /// </summary>
    [Route("/api/v1/sysuser/projectCorpWageAuth")]
    [ApiController]
    [Authorize(Roles = "SysUser")]
    public class ProjectCorpWageAuthController : ControllerBase
    {
        private readonly ManagementApplicationContext applicationContext;
        private readonly IMapper mapper;
        private readonly ProjectCorpWageAuthService projectCorpWageAuthService;
        private readonly ProjectOwnAuthorizer ownAuthorizer;

        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="applicationContext">上下文</param>
        /// <param name="mapper">mapper映射</param>
        /// <param name="projectCorpWageAuthService">主业务service</param>
        public ProjectCorpWageAuthController(ManagementApplicationContext applicationContext, 
            IMapper mapper,
            ProjectCorpWageAuthService projectCorpWageAuthService, 
            ProjectOwnAuthorizer ownAuthorizer)
        {
            this.applicationContext = applicationContext;
            this.mapper = mapper;
            this.projectCorpWageAuthService = projectCorpWageAuthService;
            this.ownAuthorizer = ownAuthorizer;
        }

        /// <summary>
        /// 获取项目参建企业非正常工资单授权列表(分页)
        /// </summary>
        /// <param name="query">查询条件</param>
        /// <param name="pageNum">页码</param>
        /// <param name="pageSize">每页数量</param>
        /// <returns></returns>
        [HttpGet("page")]
        public ApiResult<PageDto<ProjectCorpWageAuthDto>> GetProjectCorpWageAuths([FromQuery] ProjectCorpWageAuthQuery query, int pageNum = 1, int pageSize = 20)
        {
            var data = projectCorpWageAuthService.GetProjectCorpWageAuths(query, pageNum, pageSize);
            var dtos = data.AsPageDto(source => { return source.AsDto(); });
            return ApiResult<PageDto<ProjectCorpWageAuthDto>>.Success(dtos);
        }

        /// <summary>
        /// 创建项目参建企业非正常工资单授权信息
        /// </summary>
        /// <param name="form">表单数据</param>
        /// <returns></returns>
        [HttpPost("create")]
        [PermissionCheck("projectCorpWageAuth:add")]
        public ApiResult Add([FromBody] ProjectCorpWageAuthEditForm form)
        {
            ProjectCorpWageAuth entity = new();
            mapper.Map(form, entity);
            entity.CreateTime = DateTime.Now;
            projectCorpWageAuthService.Create(entity);
            return ApiResult.Success("创建成功");
        }

        /// <summary>
        /// 项目参建企业非正常工资单授权信息表单数据
        /// </summary>
        /// <param name="id">id</param>
        /// <returns></returns>
        [HttpGet("{id}/form")]
        [PermissionCheck("projectCorpWageAuth:details")]
        public ApiResult<ProjectCorpWageAuthEditForm> GetRole(int id)
        {
            var data = projectCorpWageAuthService.Get(id);
            if (data == null)
                return ApiResult<ProjectCorpWageAuthEditForm>.Failure("项目参建企业非正常工资单授权信息数据不存在");
            return ApiResult<ProjectCorpWageAuthEditForm>.Success(data.AsForm());
        }

        /// <summary>
        /// 修改项目参建企业非正常工资单授权信息
        /// </summary>
        /// <param name="id">id</param>
        /// <param name="form">表单信息</param>
        /// <returns></returns>
        [HttpPut("{id}")]
        [PermissionCheck("projectCorpWageAuth:edit")]
        public ApiResult Update(int id, [FromBody] ProjectCorpWageAuthEditForm form)
        {
            var data = projectCorpWageAuthService.Get(id);
            if (data == null) return ApiResult.Failure("项目参建企业非正常工资单授权信息不存在");

            #region 验证数据权限

            ownAuthorizer.CheckAndThrowIfOwnAuthenticationFailed(data.ProjectId);

            #endregion 验证数据权限

            mapper.Map(form, data);
            projectCorpWageAuthService.Update(data);
            return ApiResult.Success("修改成功");
        }

        /// <summary>
        /// 删除项目参建企业非正常工资单授权信息
        /// </summary>
        /// <param name="ids">id集合，多个用,隔开</param>
        /// <returns></returns>
        [HttpDelete("{ids}")]
        [PermissionCheck("projectCorpWageAuth:delete")]
        public ApiResult Delete(string ids)
        {
            var datas = projectCorpWageAuthService.GetProjectCorpWageAuths(ValueUtility.ConvertToList<int>(ids));

            #region 验证数据权限

            ownAuthorizer.CheckAndThrowIfOwnAuthenticationFailed(datas.Select(i => i.ProjectId));

            #endregion 验证数据权限

            projectCorpWageAuthService.Deleted(ValueUtility.ConvertToList<int>(ids));
            return ApiResult.Success("删除成功");
        }
    }
}